package com.lai.gulimall.cart.vo;


import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车  结构为 Map<String(k1),Map<Long(k2),CartItemVo>>    k1:用户id，  k2: skuid
 */
public class CartVo implements Serializable {

    /**
     * 全部购物项
     */
    List<CartItemVo> items;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     *商品类型数量
     */
    private Integer countType;
    /**
     *商品总价
     */
    private BigDecimal totalAmount;
    /**
     *减免价格
     */
    private BigDecimal reduce = new BigDecimal("0.00");

    public List<CartItemVo> getItems() {
        return items;
    }

    public void setItems(List<CartItemVo> items) {
        this.items = items;
    }

    /**
     * 获取全部商品数量
     */
    public Integer getCountNum() {
        return this.items != null && this.items.size() > 0 ? this.items.stream().mapToInt(CartItemVo::getCount).sum() : 0;
    }


    public Integer getCountType() {
        return this.items != null && this.items.size() > 0 ? this.items.size() : 0;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0.00");
        /*return this.items != null && this.items.size() > 0 ?this.items.stream().map(CartItemVo::getTotalPrice).reduce(bigDecimal,BigDecimal::add) : bigDecimal;*/
        //1、计算购物项总价
        if(this.items != null && this.items.size() > 0){
            for (CartItemVo item : items) {
                if(item.getCheck()){
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }
        //2、减去优惠总价
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
